home *** CD-ROM | disk | FTP | other *** search
/ Chip 2006 June / CHIP 2006-06.2.iso / program / freeware / Democracy-0.8.2.exe / xulrunner / python / BitTorrent / Uploader.py < prev    next >
Encoding:
Python Source  |  2006-04-10  |  3.2 KB  |  96 lines

  1. # The contents of this file are subject to the BitTorrent Open Source License
  2. # Version 1.0 (the License).  You may not copy or use this file, in either
  3. # source code or executable form, except in compliance with the License.  You
  4. # may obtain a copy of the License at http://www.bittorrent.com/license/.
  5. #
  6. # Software distributed under the License is distributed on an AS IS basis,
  7. # WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
  8. # for the specific language governing rights and limitations under the
  9. # License.
  10.  
  11. # Written by Bram Cohen
  12.  
  13. from BitTorrent.CurrentRateMeasure import Measure
  14.  
  15.  
  16. class Upload(object):
  17.  
  18.     def __init__(self, connection, ratelimiter, totalup, totalup2, choker,
  19.                  storage, max_slice_length, max_rate_period):
  20.         self.connection = connection
  21.         self.ratelimiter = ratelimiter
  22.         self.totalup = totalup
  23.         self.totalup2 = totalup2
  24.         self.choker = choker
  25.         self.storage = storage
  26.         self.max_slice_length = max_slice_length
  27.         self.max_rate_period = max_rate_period
  28.         self.choked = True
  29.         self.unchoke_time = None
  30.         self.interested = False
  31.         self.buffer = []
  32.         self.measure = Measure(max_rate_period)
  33.         if storage.do_I_have_anything():
  34.             connection.send_bitfield(storage.get_have_list())
  35.  
  36.     def got_not_interested(self):
  37.         if self.interested:
  38.             self.interested = False
  39.             del self.buffer[:]
  40.             self.choker.not_interested(self.connection)
  41.  
  42.     def got_interested(self):
  43.         if not self.interested:
  44.             self.interested = True
  45.             self.choker.interested(self.connection)
  46.  
  47.     def get_upload_chunk(self):
  48.         if not self.buffer:
  49.             return None
  50.         index, begin, length = self.buffer.pop(0)
  51.         piece = self.storage.get_piece(index, begin, length)
  52.         if piece is None:
  53.             self.connection.close()
  54.             return None
  55.         self.measure.update_rate(len(piece))
  56.         self.totalup.update_rate(len(piece))
  57.         self.totalup2.update_rate(len(piece))
  58.         return (index, begin, piece)
  59.  
  60.     def got_request(self, index, begin, length):
  61.         if not self.interested or length > self.max_slice_length:
  62.             self.connection.close()
  63.             return
  64.         if not self.connection.choke_sent:
  65.             self.buffer.append((index, begin, length))
  66.             if self.connection.next_upload is None and \
  67.                    self.connection.connection.is_flushed():
  68.                 self.ratelimiter.queue(self.connection)
  69.  
  70.     def got_cancel(self, index, begin, length):
  71.         try:
  72.             self.buffer.remove((index, begin, length))
  73.         except ValueError:
  74.             pass
  75.  
  76.     def choke(self):
  77.         if not self.choked:
  78.             self.choked = True
  79.             self.connection.send_choke()
  80.  
  81.     def sent_choke(self):
  82.         assert self.choked
  83.         del self.buffer[:]
  84.  
  85.     def unchoke(self, time):
  86.         if self.choked:
  87.             self.choked = False
  88.             self.unchoke_time = time
  89.             self.connection.send_unchoke()
  90.  
  91.     def has_queries(self):
  92.         return len(self.buffer) > 0
  93.  
  94.     def get_rate(self):
  95.         return self.measure.get_rate()
  96.